package product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import product.domain.SpuInfo;
import product.service.SpuInfoService;
import org.springframework.web.bind.annotation.*;
import product.utils.GetUtils;
import product.vo.SpuSaveVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * spu信息(SpuInfo)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:17
 */
@RestController
@RequestMapping("product/spuInfo")
public class SpuInfoController {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    GetUtils getUtils;

    @GetMapping("list")
    public R list(HttpServletRequest request) {
        Map<String, Object> params = getUtils.getParamsWithMapByRequest(request);
        Page<SpuInfo> skuInfoPage = spuInfoService.querPage(params);
        return R.ok().put("data", skuInfoPage);
    }

    @PostMapping("save")
    public R spuSaveVo(@RequestBody SpuSaveVo spuSaveVo) {
        spuInfoService.saveSpuInfo(spuSaveVo);
        return R.ok("商品发布成功！");
    }

    /**
     * 商品上架
     *
     * @return
     */
    @PutMapping("{id}/up")
    public R spuUp(@PathVariable Long id) {
        spuInfoService.spuUP(id);
        return R.ok("商品上架成功");
    }

    @GetMapping("findSpu/{skuId}")
    SpuInfo findSpuInfoBySkuId(@PathVariable("skuId") Long skuId) {
        return spuInfoService.findSpuInfoBySkuId(skuId);
    }

}

